using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CrystalSkillController : MonoBehaviour
{
    private float destoryTimer;
    private bool canBoom;
    private Animator ani => GetComponentInChildren<Animator>();
    private CircleCollider2D collider2 => GetComponent<CircleCollider2D>();
    private bool canGrow;
    private float growSpeed;
    private float growSize;
    private Transform moveTarget;
    private bool canMove;
    private float moveSpeed;
    [SerializeField] private LayerMask enemyLayer;
    private Transform enemyTarget;
    private Inventory inventory;
    private void Update()
    {
        destoryTimer -= Time.deltaTime;
        if (destoryTimer < 0)
        {
            FinishCrystal();
        }
        Move();
    }
    private void Start()
    {
        inventory = Inventory.Instance;
    }

    public void FinishCrystal()
    {
        if (canBoom)
        {
            Grow();
            ani.SetTrigger("Boom");
        }
        else
            DestroySelf();
    }
    private void Move()
    {
        if (canMove&&moveTarget!=null)
        {
            transform.position = Vector2.Lerp(transform.position, moveTarget.position, moveSpeed * Time.deltaTime);
            if (Vector2.Distance(transform.position, moveTarget.position) < .5f)
            {
                FinishCrystal();
                canMove = false;
            }
        }
    }
    public void FindEnemyInLayer()
    {
        float radius = SkillManager.Instance.blackDongSkill.GetBlackDongScale();
        Collider2D[] enemyArray = Physics2D.OverlapCircleAll(transform.position, radius, enemyLayer);
        if (enemyArray.Length == 0) return;
        enemyTarget = enemyArray[Random.Range(0, enemyArray.Length)].transform;
    }
    private void Grow()
    {
        if (canGrow)
            gameObject.transform.localScale = Vector2.Lerp(gameObject.transform.localScale, new Vector2(growSize, growSize), growSpeed * Time.deltaTime);
    }
    public void SetUpCryStal(float destoryTimer, bool canBoom, bool canGrow, float growSpeed, float growSize,bool canMove,float moveSpeed, Transform moveTarget)
    {
        this.destoryTimer = destoryTimer;
        this.canBoom = canBoom;
        this.canGrow = canGrow;
        this.growSpeed = growSpeed;
        this.growSize = growSize;
        this.moveTarget = moveTarget;
        this.canMove = canMove;
        this.moveSpeed = moveSpeed;
    }
    public void DestroySelf() => Destroy(gameObject);
    public void DamageTrigger()
    {
        Collider2D[] colliderArray = Physics2D.OverlapCircleAll(transform.position, collider2.radius);
        foreach (var hit in colliderArray)
            if (hit.GetComponent<Enemy>() != null)
            {
                hit.GetComponent<Entity>().SetKnockBack(transform);
                PlayerManager.Instance.player.stat.DoMagicalDamage(hit.GetComponent<StatCharacter>());
                EquipmentDamage(hit);
            }
    }
    public void EquipmentDamage(Collider2D hit)
    {
        inventory.CanUseEquipment(EquipmentitemType.Amulet,ref inventory.amuletlastUseTime,hit.transform); 
    }

    protected virtual void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(transform.position, SkillManager.Instance.blackDongSkill.GetBlackDongScale());
    }
}
